package com.ljy.jymail.order.interceptor;

import com.ljy.common.constant.AuthServerConstant;
import com.ljy.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> LoginUser = new ThreadLocal<>();


    /*
    * 登录检测
    * */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //逻辑执行之前先进行匹配，如果是服务之间的路径，则直接放行
        String uri = request.getRequestURI();
//        System.out.println(uri);
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match("/order/order/status/**", uri);
        boolean match1 = antPathMatcher.match("/payed/notify", uri);
//        System.out.println("match1的值:"+match1);
        if (match || match1) {
            return true;
        }



        MemberRespVo attribute = (MemberRespVo)request.getSession().getAttribute(AuthServerConstant.LOGIN_SUCCESS_USER);
        if (null != attribute && attribute != null){
            LoginUser.set(attribute);
            //登录了
            return true;
        }else {
            //没登陆，就去登录
            request.getSession().setAttribute("msg","请先进行登录");
            response.sendRedirect("https://48u4939222.eicp.vip/auth/login.html");
            return false;
        }

    }
}
